package org.example.agent.task;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSON;
import org.example.constant.CommonConstant;
import org.example.dto.TaskStatus;
import org.example.enums.EDeployTaskStep;
import org.example.enums.EJobType;
import org.example.enums.ETaskErrCode;
import org.example.model.UnzipFileParam;
import org.example.task.HttpTask;
import org.example.task.ITask;
import org.example.util.ThreadUtil;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUnzipTask extends ParentTask implements ITask {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUnzipTask.class);

    private UnzipFileParam unzipParam;

    private Result<TaskStatus> taskResult;

    public FileUnzipTask() {
    }

    public FileUnzipTask(String params) {
        this.unzipParam = JSON.parseObject(HexUtil.decodeHexStr(params, CharsetUtil.CHARSET_UTF_8), UnzipFileParam.class);
    }

    @Override
    public Result<TaskStatus> execute() {
        LOGGER.info("FileUnzipTask 。。。start ,params={}", unzipParam.toString());
        Result<TaskStatus> result = Result.ok();
        ThreadUtil.threadWaitSeconds(10);
        this.taskResult = result;
        LOGGER.info("FileUnzipTask 。。。end");
        return result;
    }

    @Override
    public Result<String> reportStatus() {
        this.taskResult = handleResult(this.taskResult, ETaskErrCode.FILEUNZIP);
        TaskStatus taskStatus = taskResult.getResult();
        taskStatus.setJobId(unzipParam.getJobId());
        taskStatus.setTaskId(unzipParam.getTaskId());
        taskStatus.setJobType(EJobType.DEPLOY);
        taskStatus.setTaskStep(EDeployTaskStep.UNZIP.getStepNum());
        this.taskResult.setResult(taskStatus);
        Result<String> result = null;
        HttpTask httpTask = new HttpTask(CommonConstant.MASTER_HOST_URL + "admincenter/plan/statereport", JSON.toJSONString(taskResult));
        result = httpTask.execute();
        printErrMsg(result, EJobType.DEPLOY, EDeployTaskStep.UNZIP);
        LOGGER.info("FileUnzipTask reportStatus taskResult={}",taskResult);
        return result;
    }


}
